﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using DICE_GS;

public class GameRewardResultWindow : MonoBehaviour {
	public RewardItemList itemListManager;
	public RewardPointList pointListManager;

	public RewardPointCell[] rewardPointCell;

	List<MyUnitCard> unitList = new List<MyUnitCard>();
	List<RewardItemCell> itemList = new List<RewardItemCell>();

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
	
	}

	public void SetWindowInfo(List<int> rewardPointList, List<int>rewardPointDeltaList, List<int> rewardItemList, List<int> rewardItemDeltaList, int unitCardCount, int[] unitCardIdList)
	{
		for(int i=0; i<rewardPointCell.Length; i++)
		{
			//0 - EXP
			//1 - Gold
			//2 - GachaPoint
			//3 - ELORanking
			//4 - PvPPoint
	
			RewardPointCell cell = rewardPointCell[i];
//			RewardPointCell cell = LTPrefabManager.InstantiateC(PrefabTypes.RewardPointCell, Vector3.zero, Quaternion.identity) as RewardPointCell;
//			Transform cellTrans = cell.transform;
//			cellTrans.parent = pointListManager.transform;
//			cellTrans.localScale = Vector3.one;

			if( i == 0 )
				cell.pointTypeIcon.spriteName = "Text_Xp";
			else if( i == 1 )
				cell.pointTypeIcon.spriteName = "icon_shop_gold_pack_01";
			else if( i == 2 )
				cell.pointTypeIcon.spriteName = "Text_Sp";
			else if( i == 3 )
				cell.pointTypeIcon.spriteName = null;
			else if( i == 4 )
				cell.pointTypeIcon.spriteName = null;

			cell.amountLabel.text = rewardPointDeltaList[i].ToString();
		}

		for(int i=itemList.Count; i<rewardItemDeltaList.Count; i++)
		{
			//0 - Diamond
			//1 - GachaTicket
			if( rewardItemDeltaList[i] == 0 ) continue;

			RewardItemCell cell = LTPrefabManager.InstantiateC(PrefabTypes.RewardItemCell, Vector3.zero, Quaternion.identity) as RewardItemCell;
			Transform cellTrans = cell.transform;
			cellTrans.parent = itemListManager.transform;
			cellTrans.localScale = Vector3.one;
			cellTrans.localPosition = Vector3.zero;

			if( i == 0 )
				cell.itemIcon.spriteName = "IconCash";
			else if( i == 1 )
				cell.itemIcon.spriteName = null;

			int amount = rewardItemDeltaList[i];
			int numIndex = 6;

			while( amount != 0 )
			{
				if( numIndex < 4 ) cell.amountImages[7].gameObject.SetActive(true);
				if( numIndex < 1 ) cell.amountImages[8].gameObject.SetActive(true);
				int priceNum = amount % 10;
				cell.amountImages[numIndex].spriteName = "No_B"+priceNum;
				cell.amountImages[numIndex].gameObject.SetActive(true);
				amount = amount / 10;
				numIndex--;
			}

			itemList.Add(cell);
		}

		for(int j=unitList.Count; j<unitCardCount; j++)
		{
			RewardItemCell cell = LTPrefabManager.InstantiateC(PrefabTypes.RewardItemCell, Vector3.zero, Quaternion.identity) as RewardItemCell;
			Transform cellTrans = cell.transform;
			cellTrans.parent = itemListManager.transform;
			cellTrans.localScale = Vector3.one;

			CUnitData unitData = CUnitDataManager.Instance.GetUnitData(unitCardIdList[j]);
			if( unitData != null )
			{
				CUnitAbilityItem plusAbility = CUnitAbilityDataManager.Instance.GetUnitAbilityData("1_" + unitData.unit_class);
				if( plusAbility != null )
				{
					cell.unitCard.SetCardInfo(j, 1, unitData, plusAbility);
					cell.unitCard.gameObject.SetActive(true);
					cell.itemIcon.gameObject.SetActive(false);
				}
			}
			unitList.Add(cell.unitCard);
		}

		Vector2 range = itemListManager.transform.parent.GetComponent<UIPanel>().clipOffset;
		range.x = 0f;
		itemListManager.transform.parent.GetComponent<UIPanel>().clipOffset = range;
		itemListManager.transform.parent.GetComponent<UIScrollView>().ResetPosition();
		Vector3 temp = itemListManager.transform.parent.localPosition;
		temp.x = 0f;
		itemListManager.transform.parent.localPosition = temp;

		itemListManager.GetComponent<UIGrid>().Reposition();
//		itemListManager.transform.parent.GetComponent<UIScrollView>().ResetPosition();
	}

	void OnClickedOkButton()
	{
		StageManagerWrapper.SendLeave();
	}

	bool isLerpValueEnd = false;
	void LerpValue(UISlider obj, int max)
	{
		LerpValue(obj, 0, max);
	}
	
	void LerpValue(UISlider obj, int min, int max)
	{
		StartCoroutine(Co_LerpValue(obj, min, max));
	}
	
	IEnumerator Co_LerpValue(UISlider obj, int min, int max)
	{
		yield return null;
		
		float step = 1/3.0f;
		float progress = 0f;
		
		while( progress < 1f )
		{
			SetNumber(obj, 2f * progress, min, max);
			progress += step*Time.deltaTime;
			yield return null;
		}
		isLerpValueEnd = true;
	}
	
	void SetNumber(UISlider obj, float f, int min, int max)
	{
		int num = Mathf.FloorToInt( Mathf.Lerp((float)min, (float)max, f) );
		obj.value = num;
	}
}
